package com.order.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.order.admin.bean.Menu;
import com.order.admin.bean.MenusOrder;
import com.order.admin.mapper.MenusOrderMapper;
import com.order.admin.mapper.OrderMapper;
import com.order.admin.service.MenusOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MenusOrderServiceimpl extends ServiceImpl<MenusOrderMapper,MenusOrder> implements MenusOrderService{
    @Autowired
    MenusOrderMapper menusOrderMapper;
    public void addMenu(String O_id,String M_id,String number) {
        System.out.println("O_id"+O_id);
        System.out.println("M_id"+M_id);
        MenusOrder menusOrder = new MenusOrder();
        Map<String,String> map = new HashMap<>();
        map.put("O_id",O_id);
        map.put("M_id",M_id);
        QueryWrapper<MenusOrder> queryWrapper = new QueryWrapper<MenusOrder>();
        queryWrapper.allEq(map);
        menusOrder = menusOrderMapper.selectOne(queryWrapper);
        System.out.println("menusOrder："+menusOrder);
        if(menusOrder == null){
            System.out.println("插入操作");
            MenusOrder savaMenuOrder = new MenusOrder();
            savaMenuOrder.setO_id(O_id);
            savaMenuOrder.setM_id(M_id);
            savaMenuOrder.setM_number(number);
            this.save(savaMenuOrder);
        } else {
            System.out.println("更新操作");
            UpdateWrapper<MenusOrder> objectUpdateWrapper = new UpdateWrapper<>();
            menusOrder.setM_number(Integer.toString(Integer.parseInt(menusOrder.getM_number())+Integer.parseInt(number)));
            objectUpdateWrapper.allEq(map).set("M_number",menusOrder.getM_number());
            this.update(objectUpdateWrapper);
        }
    }
}
